package proj.pt.web;

import java.beans.PropertyEditorSupport;

import org.springframework.util.StringUtils;

import proj.pt.model.Course;
import proj.pt.services.LogicProcessor;

public class CourseEditor extends PropertyEditorSupport {

	private LogicProcessor lp;
	
	public CourseEditor(LogicProcessor lp) {
		this.lp = lp;
	}

	public String getAsText() {
		Course sc = (Course)getValue();
		if (sc != null) return sc.getId().toString();
		else return "";
	}

	public void setAsText(String id) throws IllegalArgumentException {
		if(StringUtils.hasText(id)){
			long lId = Long.parseLong(id);
			Course sc = lp.getCourse(lId);
			setValue(sc);
		} else setValue(null);
	}
}
